package com.ciwong.epaper.modules.share;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.share.bean.ShareMsgObj;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzonePublish;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by young on 2017/4/6.
 * qq share helper
 */
public class QQShareHelper {
    /**
     * QQAPI
     */
    private Tencent mTencent;

    private static volatile QQShareHelper instance;
    private Context mContext;
    private IUiListener mUiListener;

    private QQShareHelper() {

    }

    public static QQShareHelper getInstance() {
        if (instance == null) {
            synchronized (QQShareHelper.class) {
                if (instance == null) {
                    instance = new QQShareHelper();
                }
            }
        }
        return instance;
    }

    public void initQQshare(Context mContext, IUiListener mUiListenter) {
        try {


            this.mContext = mContext;
            this.mUiListener = mUiListenter;
            mTencent = Tencent.createInstance(EConstants.QQ_APP_ID,
                    mContext);

        } catch (Exception e) {
            e.getStackTrace();
        }
    }


    public boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * qq回调方法 须在activity,onActivityResult方法内调用
     */
    public void onActivityResultData(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode, resultCode, data, mUiListener);
    }

    /**
     * 因为QQ空间目前只支持图文分享  所以QQ只做了图文分享
     *
     * @param shareMsgObj
     * @param qqShareListener
     */
    public void shareToQQ(final Activity mActivity, ShareMsgObj shareMsgObj, final QQShareListener qqShareListener) {
        if ("".equals(EConstants.QQ_APP_ID) || null == EConstants.QQ_APP_ID) {
            return;
        }
        if (isQQClientAvailable(mActivity)) {

        } else {
            Toast.makeText(mActivity, "尚未安装QQ,请安装QQ后再试", Toast.LENGTH_SHORT).show();
            return;
        }
        initQQshare(mActivity, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                if (qqShareListener != null) qqShareListener.onQQShareSucc(o.toString());
            }

            @Override
            public void onError(UiError uiError) {
//                ToastUtil.INSTANCE.toastCenter(mContext, "分享错误", R.mipmap.ic_error);
                if (qqShareListener != null) qqShareListener.onQQShareFailure(uiError);
            }

            @Override
            public void onCancel() {
                boolean flag=false;
                if (qqShareListener != null) {
                    flag= qqShareListener.onQQShareCancel();
                }
                if (!flag)//当取消分享 返回false时才弹次提示，当返回true时 有实现此接口处实现
                    ToastUtil.INSTANCE.toastCenter(mContext, "分享取消", R.mipmap.ic_cancel);
            }
        });
        if (shareMsgObj == null) {
            return;
        }
        Bundle bundle = new Bundle();
        switch (shareMsgObj.getShareType()) {
            case ShareMsgObj.ShareType.SHARE_WEB://图文分享
                bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
                bundle.putString(QQShare.SHARE_TO_QQ_TITLE, shareMsgObj.getMsgTitle());
                bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareMsgObj.getMsgDesription());
                bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareMsgObj.getUrl());

//                if(null != shareMsgObj.getImg_url())
                bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareMsgObj.getImg_url());

                if (null != shareMsgObj.getLocalImgUrl()) {
                    bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, shareMsgObj.getLocalImgUrl());
                }

                bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, mActivity.getString(R.string.app_name));
                mTencent.shareToQQ(mActivity, bundle, mUiListener);
                break;
            case ShareMsgObj.ShareType.SHARE_IMAGE://图片分享
                bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
                bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, shareMsgObj.getLocalImgUrl().toString());
                bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, shareMsgObj.getText().toString());
                bundle.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
                mTencent.shareToQQ(mActivity, bundle, mUiListener);
                break;
            default:
                break;
        }
    }

    /**
     * 分享到QQ空间
     *
     * @param shareMsgObj
     * @param qqShareListener
     */
    public void shareToQZone(final Activity mActivity, ShareMsgObj shareMsgObj, final QQShareListener qqShareListener) {
        if ("".equals(EConstants.QQ_APP_ID) || null == EConstants.QQ_APP_ID) {
            return;
        }
        if (isQQClientAvailable(mActivity)) {

        } else {
            Toast.makeText(mActivity, "尚未安装QQ,请安装QQ后再试", Toast.LENGTH_SHORT).show();
            return;
        }
        initQQshare(mActivity, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                if (qqShareListener != null) qqShareListener.onQQShareSucc(o.toString());
            }

            @Override
            public void onError(UiError uiError) {
//                ToastUtil.INSTANCE.toastCenter(mContext, "分享错误", R.mipmap.ic_error);
                if (qqShareListener != null) qqShareListener.onQQShareFailure(uiError);
            }

            @Override
            public void onCancel() {
                boolean flag=false;
                if (qqShareListener != null) {
                    flag= qqShareListener.onQQShareCancel();
                }
                if (!flag)//当取消分享 返回false时才弹次提示，当返回true时 有实现此接口处实现
                    ToastUtil.INSTANCE.toastCenter(mContext, "分享取消", R.mipmap.ic_cancel);
            }
        });
        if (shareMsgObj == null) {
            return;
        }
        Bundle bundle = new Bundle();
        switch (shareMsgObj.getShareType()) {
            case ShareMsgObj.ShareType.SHARE_WEB://图文分享
                bundle.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                bundle.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareMsgObj.getMsgTitle());
                bundle.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareMsgObj.getMsgDesription());
                bundle.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareMsgObj.getUrl());
                if (null != shareMsgObj.getLocalImgUrl()) {
                    bundle.putString(QzoneShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, shareMsgObj.getLocalImgUrl());
                }
                ArrayList<String> arrayList = new ArrayList<String>();
                arrayList.add(shareMsgObj.getImg_url());
                bundle.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, arrayList);
                mTencent.shareToQzone(mActivity, bundle, mUiListener);

            case ShareMsgObj.ShareType.SHARE_IMAGE://图片分享

                bundle.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE);
                bundle.putString(QzoneShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, shareMsgObj.getLocalImgUrl().toString());
                bundle.putString(QzoneShare.SHARE_TO_QQ_APP_NAME, shareMsgObj.getText().toString());
                bundle.putInt(QzoneShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
                mTencent.shareToQQ(mActivity, bundle, mUiListener);
                break;
            default:
                break;
        }
    }

    public void release() {
        if (this.mTencent != null) {
            this.mTencent = null;
        }
        this.mUiListener = null;
        this.mContext = null;
        instance = null;
    }

}
